package com.tanhua.app.controller;

import com.tanhua.app.service.UserService;
import com.tanhua.db.User;
import com.tanhua.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping(value = "/saveUser",name="保存用户")
    public ResponseEntity saveUser(@RequestBody User user){
        return userService.saveUser(user);
    }

    @GetMapping(value = "/findByPhone",name="根据手机号做用户查询")
    public ResponseEntity findByPhone(String  phone){
        return userService.findByPhone(phone);
    }


    @PostMapping(value = "/login",name="手机登录发送验证码")
    public ResponseEntity login(@RequestBody Map<String,String> map){
        // 1 接受获取手机号
        String phone = map.get("phone");
        // 2 响应
        return userService.login(phone);
    }


    @PostMapping(value = "/loginVerification",name="注册/登录")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> map){
        // 1 接受获取手机号/验证码
        String phone = map.get("phone");
        String yzm = map.get("verificationCode");
        // 2 响应
        return userService.loginVerification(phone,yzm);
    }

    @PostMapping(value = "/loginReginfo",name="完善用户信息上集")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
        return userService.loginReginfo(userInfo,token);
    }


    @PostMapping(value = "/loginReginfo/head",name="完善用户信息下集")
    public ResponseEntity head(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        return userService.head(headPhoto,token);
    }


}
